package edu.washington.ling.syntric.struct.rule;

import java.util.LinkedList;
import java.util.List;

import edu.stanford.nlp.util.StringUtils;
import edu.washington.ling.syntric.struct.tree.Node;

public class BiPSPattern implements Comparable<BiPSPattern>{
	private List<Node> psSpine;
	private List<Node> psRootChain;
	private Node psHeadChild;
	private Node psDepChild;
	
	public BiPSPattern(List<Node> rootChain, Node depChild, Node headChild) {
		psSpine = new LinkedList<Node>(rootChain);
		psSpine.add(headChild);
		psRootChain = rootChain;
		psHeadChild = headChild;
		psDepChild = depChild;
	}
	public List<Node> getSpine() {
		return psSpine;
	}
	public Node getRoot() {
		return psRootChain.get(0);
	}
	public List<Node> getRootChain() {
		return psRootChain;
	}
	public Node getDepChild() {
		return psDepChild;
	}
	public Node getHeadChild() {
		return psHeadChild;
	}
	
	public String getSpineStr() {
		List<String> spineStrs = new LinkedList<String>();
		for (Node psSpineElem: psSpine) {
			spineStrs.add(psSpineElem.getLabel());
		}
		return StringUtils.join(spineStrs, " ");
	}
	public boolean equals(Object other){
		if (this == other){
			return true;
		}
		if (!(other instanceof BiPSPattern)) {
			return false;
		}
		BiPSPattern otherPattern = (BiPSPattern) other;
		return (psRootChain.equals(otherPattern.psRootChain) && psHeadChild.equals(otherPattern.psHeadChild) 
				&& psDepChild.equals(otherPattern.psDepChild)); 
	}
	@Override
	public int hashCode() {
		return (psRootChain.hashCode() + 17*psHeadChild.hashCode() + 31*psDepChild.hashCode());
	}
	@Override
	public int compareTo(BiPSPattern otherPatt) {
		LinkedList<Node> spineQ = new LinkedList<Node>(psSpine);
		LinkedList<Node> otherSpineQ = new LinkedList<Node>(otherPatt.psSpine);
		int cmp = 0;
		while (spineQ.size() > 0 && otherSpineQ.size() > 0) {
			cmp = spineQ.poll().compareTo(otherSpineQ.poll());
			if (cmp != 0) { break; }
		}
		return cmp;
	}
}
